<?php

/**
 * Description of CrawlerManager
 *
 * @author bonfiglio
 */
class CrawlerManager {

    private $stopped = false;
    public $get = array();
    public $Crawler = null;
    public $mode = 'cron';
    
    public function __construct($get,$cron_mod) {                
        $crawler = false;
        
        if(isset($get['crawler'])){
            $crawler = filter_var($get['crawler']);
            unset($get['crawler']);
            $get = array_values($get);
            $this->mode = 'http';
        }elseif(isset($get[1])){
            $this->mode = 'cron';
            $crawler = filter_var($get[1]);
            unset($get[0]);
            unset($get[1]);
        }
        
        $this->log('CrawlerName (' . $crawler . ')');
        
        //Gestion d'erreurs
        if ($crawler && class_exists($crawler)) {
            $this->Crawler = $crawler;
            $this->get = $get;
            $this->log('new Crawler Manager successfully started');
        } else {
            $this->stopped = true;
            if (!$crawler) {
                $this->log('no crawler given', 'warning');
            } elseif (!class_exists($crawler)) {
                $this->log('unkowned crawler', 'warning');
            }
            $Mailer = new CrawlerMailer(array('to' => array('Adriano' => 'bonfiglio@certinergy.com')));
            $Mailer->Subject = 'CrawlerManager alert';
            $Mailer->Body = '<pre>$get : ' . implode(',',$get) . '</pre><pre>$cron_mod :' . $cron_mod . '</pre>';
            $Mailer->Send();
        }
    }
    
    public function log($message, $level = 'log'){
        if($this->mode == 'cron'){
            Log::crawler($message,$this->mode,$level);
        }else{
            Log::pr($message);
        }
    }
    
    public function isStopped(){
        return $this->stopped;
    }
}

?>
